#include "ConditionalGate.h"

#include "Events\Event.h"

// Constructor
ConditionalGate::ConditionalGate(void)
	: Gate()
{
	this->xCount = 0;
	this->yCount = 0;
	this->maxXCount = 0;
	this->maxYCount = 0;
	this->startHeight = 0;
}

// Destructor
ConditionalGate::~ConditionalGate(void)
{
}

void ConditionalGate::Update(float deltaTime)
{
	if(xCount != 0 && yCount != 0)
		SetMaxHeight(startHeight);
	else if(xCount > 0)
		SetMaxHeight((int)(((maxXCount - xCount) / (float)(maxXCount)) * startHeight));
	else if(yCount > 0)
		SetMaxHeight((int)(((maxYCount - yCount) / (float)(maxYCount)) * startHeight));
	else
		SetMaxHeight(startHeight);

	Gate::Update(deltaTime);
}

void ConditionalGate::HandleEvent(Event* e)
{
	if(e->GetEventID() == "ButtonActivated")
	{
		vector<string>* tags = getSenderTags(e->GetEventID());

		string tag = "";
		GameObject* obj = (GameObject*)e->GetSender();

		for(unsigned int i = 0; i < tags->size(); ++i)
		{
			tag = obj->GetTag();
			if(tag == (*tags)[i])
			{
				if(tag == "x")
					++xCount;
				else if(tag == "y")
					++yCount;

				break;
			}
		}
	}
	else if(e->GetEventID() == "ButtonDeactivated")
	{
		vector<string>* tags = getSenderTags(e->GetEventID());

		string tag = "";
		GameObject* obj = (GameObject*)e->GetSender();

		for(unsigned int i = 0; i < tags->size(); ++i)
		{
			tag = obj->GetTag();
			if(tag == (*tags)[i])
			{
				if(tag == "x")
					--xCount;
				else if(tag == "y")
					--yCount;

				break;
			}
		}
	}
}

void ConditionalGate::SetStartHeight(unsigned int height)
{
	this->startHeight = height;
}

void ConditionalGate::SetMaxXCount(unsigned int max)
{
	this->maxXCount = max;
}

void ConditionalGate::SetMaxYCount(unsigned int max)
{
	this->maxYCount = max;
}